home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Var_Dump / Renderer / Table.php < prev    next >
PHP Script  |  2004-10-01  |  10KB  |  240 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Frederic Poeydomenge <frederic.poeydomenge@free.fr>         |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id:
  20.  
  21. require_once 'Var_Dump/Renderer/Common.php';
  22.  
  23. /**
  24.  * A concrete renderer for Var_Dump
  25.  *
  26.  * Returns a table-based representation of a variable in HTML
  27.  *
  28.  * @package Var_Dump
  29.  * @category PHP
  30.  * @author Frederic Poeydomenge <frederic.poeydomenge@free.fr>
  31.  */
  32.  
  33. class Var_Dump_Renderer_Table extends Var_Dump_Renderer_Common
  34. {
  35.  
  36.     /**
  37.      * Default configuration options.
  38.      *     show_caption     : bool,    Show the caption or not
  39.      *     show_eol         : string,  String to insert before a newline, or false
  40.      *     before_num_key   : string,  Text to insert before a numerical key
  41.      *     after_num_key    : string,  Text to insert after a numerical key
  42.      *     before_str_key   : string,  Text to insert before a string key
  43.      *     after_str_key    : string,  Text to insert after a string key
  44.      *     before_type      : string,  Text to insert before a type
  45.      *     after_type       : string,  Text to insert after a type
  46.      *     before_value     : string,  Text to insert before a value
  47.      *     after_value      : string,  Text to insert after a value
  48.      *     start_table      : string,  Text to insert before the table
  49.      *     end_table        : string,  Text to insert after the table
  50.      *     start_tr         : string,  Text to insert before a row
  51.      *     end_tr           : string,  Text to insert after a row
  52.      *     start_tr_alt     : string,  Text to insert after an alternate row
  53.      *     end_tr_alt       : string,  Text to insert after an alternate row
  54.      *     start_td_key     : string,  Text to insert before the key cell
  55.      *     end_td_key       : string,  Text to insert after the key cell
  56.      *     start_td_type    : string,  Text to insert before the type cell
  57.      *     end_td_type      : string,  Text to insert after the type cell
  58.      *     start_td_value   : string,  Text to insert before the value cell
  59.      *     end_td_value     : string,  Text to insert after the value cell
  60.      *     start_td_colspan : string,  Text to insert before a group cell
  61.      *     end_td_colspan   : string,  Text to insert after a group cell
  62.      *     start_caption    : string,  Text to insert before the caption
  63.      *     end_caption      : string,  Text to insert after the caption
  64.      * @var array
  65.      * @access public
  66.      */
  67.     var $defaultOptions = array(
  68.         'show_caption'     => TRUE,
  69.         'show_eol'         => FALSE,
  70.         'before_num_key'   => '',
  71.         'after_num_key'    => '',
  72.         'before_str_key'   => '',
  73.         'after_str_key'    => '',
  74.         'before_type'      => '',
  75.         'after_type'       => '',
  76.         'before_value'     => '',
  77.         'after_value'      => '',
  78.         'start_table'      => '<table>',
  79.         'end_table'        => '</table>',
  80.         'start_tr'         => '<tr>',
  81.         'end_tr'           => '</tr>',
  82.         'start_tr_alt'     => '<tr>',
  83.         'end_tr_alt'       => '</tr>',
  84.         'start_td_key'     => '<td>',
  85.         'end_td_key'       => '</td>',
  86.         'start_td_type'    => '<td>',
  87.         'end_td_type'      => '</td>',
  88.         'start_td_value'   => '<td>',
  89.         'end_td_value'     => '</td>',
  90.         'start_td_colspan' => '<td colspan="2">',
  91.         'end_td_colspan'   => '</td>',
  92.         'start_caption'    => '<caption>',
  93.         'end_caption'      => '</caption>'
  94.     );
  95.  
  96.     /**
  97.      * Class constructor.
  98.      * @param array $options Parameters for the rendering.
  99.      * @access public
  100.      */
  101.     function Var_Dump_Renderer_Table($options = array())
  102.     {
  103.         $this->setOptions($options);
  104.     }
  105.  
  106.     /**
  107.      * Returns the string representation of a variable
  108.      * @return string The string representation of the variable.
  109.      * @access public
  110.      */
  111.     function toString()
  112.     {
  113.         if (count($this->family) == 1) {
  114.             return $this->_toString_Single();
  115.         } else {
  116.             return $this->_toString_Array();
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Returns the string representation of a single variable
  122.      * @return string The string representation of a single variable.
  123.      * @access private
  124.      */
  125.     function _toString_Single()
  126.     {
  127.         $string = htmlspecialchars($this->value[0]);
  128.         if ($this->options['show_eol'] !== FALSE) {
  129.             $string = str_replace(
  130.                 "\n",
  131.                 $this->options['show_eol'] . "\n",
  132.                 $string
  133.             );
  134.         }
  135.         return
  136.             $this->options['start_table'] .
  137.             $this->options['start_tr'] .
  138.             $this->options['start_td_type'] .
  139.             $this->options['before_type'] .
  140.             htmlspecialchars($this->type[0]) .
  141.             $this->options['after_type'] .
  142.             $this->options['end_td_type'] .
  143.             $this->options['start_td_value'] .
  144.             $this->options['before_value'] .
  145.             nl2br($string) .
  146.             $this->options['after_value'] .
  147.             $this->options['end_td_value'] .
  148.             $this->options['end_tr'] .
  149.             $this->options['end_table'];
  150.     }
  151.  
  152.     /**
  153.      * Returns the string representation of a multiple variable
  154.      * @return string The string representation of a multiple variable.
  155.      * @access private
  156.      */
  157.     function _toString_Array()
  158.     {
  159.         $txt = '';
  160.         $stack = array(0);
  161.         $counter = count($this->family);
  162.         for ($c = 0 ; $c < $counter ; $c++) {
  163.             switch ($this->family[$c]) {
  164.                 case VAR_DUMP_START_GROUP :
  165.                     array_push($stack, 0);
  166.                     if ($this->depth[$c] > 0) {
  167.                         $txt .= $this->options['start_td_colspan'];
  168.                     }
  169.                     $txt .= $this->options['start_table'];
  170.                     if ($this->options['show_caption']) {
  171.                         $txt .=
  172.                             $this->options['start_caption'] .
  173.                             htmlspecialchars($this->value[$c]) .
  174.                             $this->options['end_caption'];
  175.                     }
  176.                     break;
  177.                 case VAR_DUMP_FINISH_GROUP :
  178.                     array_pop($stack);
  179.                     $txt .= $this->options['end_table'];
  180.                     if ($this->depth[$c] > 0) {
  181.                         $txt .=
  182.                             $this->options['end_td_colspan'] .
  183.                             $this->options['end_tr'];
  184.                     }
  185.                     break;
  186.                 case VAR_DUMP_START_ELEMENT_NUM :
  187.                 case VAR_DUMP_START_ELEMENT_STR :
  188.                     array_push($stack, 1 - array_pop($stack));
  189.                     $tr = (end($stack) == 1) ? 'start_tr' : 'start_tr_alt';
  190.                     $comp = ($this->family[$c] == VAR_DUMP_START_ELEMENT_NUM) ? 'num' : 'str';
  191.                     $txt .=
  192.                         $this->options[$tr] .
  193.                         $this->options['start_td_key'] .
  194.                         $this->options['before_'.$comp.'_key'] .
  195.                         htmlspecialchars($this->value[$c]) .
  196.                         $this->options['after_'.$comp.'_key'] .
  197.                         $this->options['end_td_key'];
  198.                     break;
  199.                 case VAR_DUMP_FINISH_ELEMENT :
  200.                 case VAR_DUMP_FINISH_STRING :
  201.                     $etr = (end($stack) == 1) ? 'end_tr' : 'end_tr_alt';
  202.                     if (!is_null($this->value[$c])) {
  203.                         $string = htmlspecialchars($this->value[$c]);
  204.                         if ($this->options['show_eol'] !== FALSE) {
  205.                             $string = str_replace(
  206.                                 "\n",
  207.                                 $this->options['show_eol'] . "\n",
  208.                                 $string
  209.                             );
  210.                         }
  211.                         $txt .=
  212.                             $this->options['start_td_type'] .
  213.                             $this->options['before_type'] .
  214.                             htmlspecialchars($this->type[$c]) .
  215.                             $this->options['after_type'] .
  216.                             $this->options['end_td_type'] .
  217.                             $this->options['start_td_value'] .
  218.                             $this->options['before_value'] .
  219.                             nl2br($string) .
  220.                             $this->options['after_value'] .
  221.                             $this->options['end_td_value'] .
  222.                             $this->options[$etr];
  223.                     } else {
  224.                         $txt .=
  225.                             $this->options['start_td_colspan'] .
  226.                             $this->options['before_type'] .
  227.                             htmlspecialchars($this->type[$c]) .
  228.                             $this->options['after_type'] .
  229.                             $this->options['end_td_colspan'] .
  230.                             $this->options[$etr];
  231.                     }
  232.                     break;
  233.             }
  234.         }
  235.         return $txt;
  236.     }
  237.  
  238. }
  239.  
  240. ?>